package com.linewow.xhyy.nihaonew.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.activity.TopicDetailActivity;
import com.linewow.xhyy.nihaonew.adapter.FindTagTopicAdapter;
import com.linewow.xhyy.nihaonew.base.BaseFragment;
import com.linewow.xhyy.nihaonew.base.BaseInfo;
import com.linewow.xhyy.nihaonew.entity.user.Uinfo;
import com.linewow.xhyy.nihaonew.entity.image.SimpleImagesInfo;
import com.linewow.xhyy.nihaonew.entity.tag.CustomTagsInfo;
import com.linewow.xhyy.nihaonew.entity.topic.FindTagTopicInfo;
import com.linewow.xhyy.nihaonew.net.api.NiHaoOkgo;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by LXR on 2016/10/10.
 */
public class MineTopicFragment extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener {
    @Bind(R.id.mine_topic_recycler)
    RecyclerView mineTopicRecycler;
    private Gson gson;
    private List<FindTagTopicInfo<Uinfo, SimpleImagesInfo, CustomTagsInfo>> findTagTopicInfos;
    private FindTagTopicAdapter adapter;
    private Type tagDetailType;
    private String TAG="MineTopicFragment";
    private Uinfo uinfo;
    private int page=1;
    private boolean userFlag;

    @Override
    public void initList() {
        super.initList();
    }

    @Override
    public View initView() {
        View view = View.inflate(getActivity(), R.layout.fragment_mine_topic, null);
        ButterKnife.bind(this, view);
        LinearLayoutManager manager = new LinearLayoutManager(MineTopicFragment.this.getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mineTopicRecycler.setLayoutManager(manager);
        mineTopicRecycler.setItemAnimator(new DefaultItemAnimator());
        mineTopicRecycler.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                startDetailActivity(((FindTagTopicInfo<Uinfo, SimpleImagesInfo, CustomTagsInfo>)baseQuickAdapter.getData().get(i)).tid);
            }
        });
        return view;
    }

    @Override
    public void initData() {
        super.initData();
        initGson();
        Bundle bundle=getArguments();

        if(bundle==null){
            userFlag=false;
            refreshData();
        }else{
            userFlag=true;
            Log.e(TAG,"其他用户的请求");
            uinfo=bundle.getParcelable("info");
            refreshUserData();
        }

    }

    private void refreshUserData() {
        page=1;
        requestUserInfo();
    }



    public void refreshData() {
        page=1;
        requestInfo();
    }



    private void initGson() {
        gson = new Gson();
        tagDetailType = new TypeToken<BaseInfo<FindTagTopicInfo<Uinfo, SimpleImagesInfo, CustomTagsInfo>>>() {
        }.getType();
    }


    @Override
    public void onLoadMoreRequested() {
        if(userFlag){
            loadUserData();
        }else{
            loadData();
        }
    }

    private void loadData() {
        page++;
        requestInfo();
    }

    private void loadUserData() {
        page++;
        requestUserInfo();

    }
    private void requestInfo() {
        NiHaoOkgo.userTopic("",page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<BaseInfo<FindTagTopicInfo<Uinfo,SimpleImagesInfo,CustomTagsInfo>>, List<FindTagTopicInfo<Uinfo,SimpleImagesInfo,CustomTagsInfo>>>() {
                    @Override
                    public List<FindTagTopicInfo<Uinfo, SimpleImagesInfo, CustomTagsInfo>> call(BaseInfo<FindTagTopicInfo<Uinfo, SimpleImagesInfo, CustomTagsInfo>> findTagTopicInfoBaseInfo) {
                        return findTagTopicInfoBaseInfo.data;
                    }
                })
                .subscribe(new Action1<List<FindTagTopicInfo<Uinfo, SimpleImagesInfo, CustomTagsInfo>>>() {
                    @Override
                    public void call(List<FindTagTopicInfo<Uinfo, SimpleImagesInfo, CustomTagsInfo>> findTagTopicInfos) {
                        if(adapter==null){
                            adapter=new FindTagTopicAdapter(findTagTopicInfos);
                            adapter.isFirstOnly(false);
                            adapter.setOnLoadMoreListener(MineTopicFragment.this);
                            mineTopicRecycler.setAdapter(adapter);
                        }else{
                            if(page!=1){//上拉加载
                                adapter.addData(findTagTopicInfos);
                                if(findTagTopicInfos.size()<5){
                                    adapter.loadComplete();
                                }
                            }else{//刷新  好像没办法 loadcompletel
                                adapter.setNewData(findTagTopicInfos);
                                adapter.openLoadMore(5);
                            }

                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Log.e(TAG, "错误信息" + throwable.getMessage());
                        showToast(getString(R.string.message_no_internet));
                    }
                });
    }

    private void requestUserInfo() {
        NiHaoOkgo.userTopic(uinfo.uid,page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<BaseInfo<FindTagTopicInfo<Uinfo,SimpleImagesInfo,CustomTagsInfo>>, List<FindTagTopicInfo<Uinfo,SimpleImagesInfo,CustomTagsInfo>>>() {
                    @Override
                    public List<FindTagTopicInfo<Uinfo, SimpleImagesInfo, CustomTagsInfo>> call(BaseInfo<FindTagTopicInfo<Uinfo, SimpleImagesInfo, CustomTagsInfo>> findTagTopicInfoBaseInfo) {
                        return findTagTopicInfoBaseInfo.data;
                    }
                })
                .subscribe(new Action1<List<FindTagTopicInfo<Uinfo, SimpleImagesInfo, CustomTagsInfo>>>() {
                    @Override
                    public void call(List<FindTagTopicInfo<Uinfo, SimpleImagesInfo, CustomTagsInfo>> findTagTopicInfos) {
                        if(adapter==null){
                            adapter=new FindTagTopicAdapter(findTagTopicInfos);
                            adapter.isFirstOnly(false);
                            adapter.setOnLoadMoreListener(MineTopicFragment.this);
                            mineTopicRecycler.setAdapter(adapter);
                        }else{
                            adapter.addData(findTagTopicInfos);
                            if(findTagTopicInfos.size()<5){
                                adapter.loadComplete();
                            }
                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Log.e(TAG, "错误信息" + throwable.getMessage());
                        showToast(getString(R.string.message_no_internet));
                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public void startDetailActivity(String tid){
        Intent intent=new Intent(MineTopicFragment.this.getActivity(), TopicDetailActivity.class);
        intent.putExtra("msg",tid);
        startActivity(intent);
    }
}
